<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>绑定手机</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/common.css" />
    <style>
        body{

        }
    </style>
</head>
<body>
	<div class="aui-content aui-margin-b-15">
	    <ul class="aui-list aui-form-list">
	        <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label-icon">
                        <i class="aui-iconfont aui-icon-mobile"></i>
                    </div>
                    <div class="aui-list-item-input">
                        <input type="tel" id="mobile" placeholder="请输入手机号" maxlength="11">
                    </div>
                </div>
            </li>
	        <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label-icon">
                        <i class="aui-iconfont aui-icon-lock"></i>
                    </div>
                    <div class="aui-list-item-input">
                        <input type="tel" placeholder="请输入验证码" autocomplete="off" id="smscode" maxlength="5">
                    </div>
                    <div class="aui-list-item-label aui-margin-r-5" style="width: 8rem;">
                        <div class="aui-btn codebtn1" style="width: 6rem;color: #999;" id="getcode" onclick="SendSms(this)">获取验证码</div>
                        <div class="aui-btn codebtn2 hide" style="width: 6rem;color: #999;" id="codejqs">计时器</div>
                    </div>
                </div>
            </li>
	    </ul>
	</div>

	<section class="aui-content-padded">
        <div class="aui-padded-10" onclick="Submit(this)" tapmode ><div class="aui-btn aui-btn-danger aui-btn-block jb" style="border-radius: 2rem;">确定提交</div></div>
    </section>
</body>
<script type="text/javascript" src="../script/api.js" ></script>
<script type="text/javascript" src="../script/zepto.min.js" ></script>
<script type="text/javascript" src="../script/common.js" ></script>
<script type="text/javascript">
    var referer_frame;
    apiready = function(){
        api.parseTapmode();
        referer_frame = api.pageParam.referer_frame||'root';
    };
    //提交
    function Submit(){
    	var mobile = $api.byId('mobile').value;
    	var smscode = $api.byId('smscode').value;
    	if (!window.regex_mobile.test(mobile)) {
    		_toast('请输入正确的手机号');
    		return false;
    	}
    	if (smscode<=0) {
    		_toast('请输入验证码');
    		return false;
    	}
    	_loading('加载中','',true);
        var fields = {values:{
            mobile:mobile,
            smscode:smscode,
            uid:api.pageParam.uid,
            token:api.pageParam.token
        }}
        var is_login_bind = api.pageParam.is_login_bind||0;
        _Ajax(window.APIServer.BindMobile,'post',fields,'json',function(ret){
        	_loading_close();
            if (ret.code==1) {
                _toast(ret.msg);
                if (is_login_bind==1) {
                    //存储用户信息
                    _SetPrefs('token', api.pageParam.token);
                    _SetPrefs('uid', api.pageParam.uid);
                    _SetPrefs('uinfo', api.pageParam.uinfo);
                    //广播事件：用户登录成功
                    _SendEvent('UserLogin');
                    //获取极光设备标识ID
                    var registrationId = _GetPrefs('registrationId');
                    //console.log('registrationId:'+ registrationId)
                    if(registrationId != ''){
                      _Ajax(window.APIServer.JPushId, 'post', {values:{regid:registrationId}}, 'json', function (ret) {
                          if (ret.code === 1) {
                            console.log(JSON.stringify(ret))
                          }
                      },function(err){
                        console.log(JSON.stringify(err))
                      });
                    }
                }else{
                    //广播事件：用户登录成功
                    _SendEvent('bind_mobile_wechat');
                }

                setTimeout(function() {
                     api.closeToWin({
                         name : referer_frame
                     });
                }, 500)
            }else{
                _toast(ret.msg);
            }

        });
    }
    //获取验证码
    function SendSms(){
    	var mobile = $api.byId('mobile').value;
    	if (!window.regex_mobile.test(mobile)) {
    		_toast('请输入正确的手机号');
    		return false;
    	}
    	_Ajax(window.APIServer.SendSms,'post',{values:{mobile:mobile,type:1}},'json',function(ret){
    		_toast(ret.msg);
    		if (ret.code==1) {
    			showtime(60);
    		}

    	});

    }
    //.计时器
	function showtime(t) {
		for ( i = 1; i <= t; i++)
		{
			window.setTimeout("update(" + i + "," + t + ")", i * 1000);
		}
	}
	function update(num, t) {
		if (num == t) {
			$api.addCls( $api.dom( '.codebtn2' ), 'hide' );
			$api.removeCls( $api.dom( '.codebtn1' ), 'hide' );
			$api.text($api.byId("getcode"), '重新获取');
		}
		else
		{
			printnr = t - num;
			$api.addCls( $api.dom( '.codebtn1' ), 'hide' );
			$api.removeCls( $api.dom( '.codebtn2' ), 'hide' );
			$api.text($api.byId("codejqs"), '('+printnr + " 's)");
		}
	}
</script>
</html>
